<?php

use GatewayWorker\Lib\Gateway;

require_once __DIR__.'/logic/RoomDetail.php';
require_once __DIR__.'/logic/config.php';
require_once __DIR__.'/logic/MessageCode.php';
require_once __DIR__ . './../ProtocolApi/ProtocolApi.php';

class RoomManager
{
    /**
     * @var object RoomDetail
     */
    public $roomList;//列表

    /**
     * RoomManager constructor.
     */
    public function __construct()
    {
        for ($i = 10001; $i < 10001+Config::$roomNumber; $i++) {
            $this->roomList[$i] = new RoomDetail($i);
        }
    }

    /**
     * @param $message
     * @return bool
     */
    public function receiveMessage($message){
        $msghead = $message['head'];
        $client_id = $message['client_id'];
        $message = $message['msg'];

        $SESSION=SessionMessage::getClientidToSession($client_id);
        if(empty($SESSION)){
            $message=ProtocolApi::returnMessage(ProtocolApi::$STATUS_ERROR,'请先登录');
            Gateway::sendToClient($client_id, $message);
            return true;
        }

        switch ($msghead){

            case ProtocolApi::$DDZ_GET_ROOM_LIST:
                $this->getRoomList($client_id);
                break;

            case ProtocolApi::$DDZ_JOIN_ROOM:
                $this->PlayerJoinRoom($client_id,$message);
                break;

            case ProtocolApi::$DDZ_GET_ROOM_DETAIL:
                $this->PlayerGetRoomDetail($client_id,$message);
                break;

        }
    }

    /**
     * 获取房间列表
     * @return array
     */
    public function getRoomList($client_id)
    {
        $keyList = [];
        foreach ($this->roomList as $key => $value) {
            $keyList[] = $this->roomList[$key]->getRoomState();
        }

        $msg = Status::returnMessage(Status::$StatusOK, ['RoomList'=>$keyList]);
        $message=ProtocolApi::returnMessage(ProtocolApi::$DDZ_GET_ROOM_LIST,$msg);
        Gateway::sendToClient($client_id, $message);
    }

    /**
     * 加入房间
     * @return bool
     */
    public function PlayerJoinRoom($client_id,$message)
    {
        $playerSession=SessionMessage::getClientidToSession($client_id);
        if($this->checkRoomStatus($client_id,$message)){
            return true;
        }

        if($playerSession['room_id']){
            $status=$this->roomList[$playerSession['room_id']]->checkPlayerIsJoinRoom($playerSession['user_id']);
            if($status){
                Gateway::joinGroup($client_id, $playerSession['room_id']);
                $message=ProtocolApi::returnMessage(ProtocolApi::$STATUS_ERROR,'您已在房间中');
                Gateway::sendToClient($client_id, $message);
                $message=ProtocolApi::returnMessage(ProtocolApi::$GAME_RECONNECT,['room_id'=>$playerSession['room_id']]);
                Gateway::sendToClient($client_id, $message);
                return true;
            }
            SessionMessage::updateClientidToSession($client_id,'room_id',0);
        }

        $playerDetail=SessionMessage::getClientidToSession($client_id);
        $this->roomList[$message['roomID']]->playerJoin($client_id,$playerDetail['player_id'],$playerDetail['player_info']);
    }


    /**
     * 获取房间明细信息
     * @param $client_id
     * @param $message
     * @return bool
     */
    public function PlayerGetRoomDetail($client_id,$message){

        if($this->checkRoomStatus($client_id,$message)){
            return true;
        }

        if($message['roomID']!=0){
            $roomID=$message['roomID'];
            $playerSession=SessionMessage::getClientidToSession($client_id);
            $status=$this->roomList[$roomID]->checkPlayerIsJoinRoom($playerSession['user_id']);
            if($status){
                $message=ProtocolApi::returnMessage(ProtocolApi::$GAME_RECONNECT,['room_id'=>$roomID]);
                Gateway::sendToClient($client_id, $message);
                Gateway::joinGroup($client_id,$roomID);
            }
        }

        $message=$this->roomList[$roomID]->getRoomState();
        $message=ProtocolApi::returnMessage(ProtocolApi::$DDZ_GET_ROOM_DETAIL,$message);
        Gateway::sendToClient($client_id, $message);
        return true;
    }

    /**
     * 通用房间状态检测
     * @param $client_id
     * @param $message
     * @return bool
     */
    public function checkRoomStatus($client_id,$message){
        if(empty($message['roomID'])){
            $message=ProtocolApi::returnMessage(ProtocolApi::$STATUS_ERROR,'请选择房间');
            Gateway::sendToClient($client_id, $message);
            return true;
        }
        if(empty($this->roomList[$message['roomID']])){
            $message=ProtocolApi::returnMessage(ProtocolApi::$STATUS_ERROR,'房间不存在');
            Gateway::sendToClient($client_id, $message);
            return true;
        }
        return false;
    }


}